package com.cacshanghai.cac_oa_client.widget;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.codetroopers.betterpickers.calendardatepicker.CalendarDatePickerDialogFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by geyh on 2016/6/2.
 */
public class DataPickView extends TextView {
    public DataPickView(Context context) {
        super(context);
        init(context);
    }

    public DataPickView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DataPickView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
                        .setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
                            @Override
                            public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
                                String dateFormat = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                                try {
                                    Date date = new SimpleDateFormat("yyyy-M-d").parse(dateFormat);
                                    ((TextView) v).setText(new SimpleDateFormat("yyyy-MM-dd").format(date));
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).setThemeLight();
                cdp.show(((FragmentActivity) context).getSupportFragmentManager(), "fragment_date_picker_name");
            }
        });
    }

}
